#include <iostream>
using std::cout;
using std::endl;

class Base{
public:
    virtual
    void show(){
        cout << "Base::show()" << endl;
    }

private:
    long _base = 10;
};

class Derived : public Base {
public:
    void show(){
        cout << "Derived::show()" << endl;
    }

private:
    long _derived = 20;
};


void test(){
    Base base;
    base.show();
    Derived derived;
    derived.show();
    cout << "============" << endl;
    //使用基类指针去指向派生类对象时，指针能够操作的范围只是基类的部分
    //所以后续去调用show，也只能调用到基类的show函数部分
    //实现多态只需要在目前的代码基础上增加一个virtual即可
    Base * pbase = &derived;
    pbase->show();
}

void test2(){
    //在没有引入virtual之前，大小分别是8和16字节
    cout << sizeof(Base) << endl;
    cout << sizeof(Derived) << endl;
    //在没有引入virtual之后，大小分别是16和24字节
    //整体偏大了八个字节---64位系统中很容易想到是指针
    //
    Derived d;
    Derived * p = &d;
    //将这个指针强转为long*指针类型
    long * pl = (long *)p;
    cout << *pl << endl;

    Base base;
    Base * pb = &base;
    long * pl2 = (long *)pb;
    cout << *pl2 << endl;
}

int main()
{
    test();
    return 0;
}

